import {
    createElement
} from './createElement'
let nextUnitOfWork
let pendingCommit

function createDOM(fiber) {
    if (fiber.type === 'text') {
        return document.createTextNode(fiber.type)
    } else {
        return document.createElement(fiber.type)
    }
}

function workLoop(deadLine) {
    while (nextUnitOfWork && deadLine.timeRemaining() > 0) {
        nextUnitOfWork = performUnitOfwork(nextUnitOfWork)
    }
    if (!nextUnitOfWork) {
        commitRoot(pendingCommit)
    }
}

function performUnitOfwork(fiber) {
    beginWork(fiber)
    if (fiber.child) {
        return fiber.child
    }
    // while (fiber.parent) {
    //     fiber.parent.effects.push(...fiber.effects, fiber)
    //     if (fiber.subling) {
    //         return fiber.subling
    //     }
    //     fiber = fiber.parent
    // }
    while (fiber.parent) {
        completeUnitOfWork(fiber)
        if (fiber.subling) {
            return fiber.subling
        }
        fiber = fiber.parent
    }
    pendingCommit = fiber
}

function completeUnitOfWork(fiber) {
    const parent = fiber.parent
    if (parent) {
        if (fiber.effectTag) {
            // debugger
            
            // if (!parent.firstEffect) {
            //     parent.firstEffect = fiber.firstEffect
            // }

            // if (!parent.lastEffect) {
            //     parent.lastEffect = fiber.lastEffect
            // }

            // if (parent.lastEffect) {
            //      parent.lastEffect.nextEffect = fiber
            // } else {
            //     parent.firstEffect = fiber
            // }
            console.log('构建fier链表', fiber)
            if(!parent.lastEffect){
                parent.lastEffect = fiber.lastEffect
            }
            
            
            if(parent.lastEffect){
                parent.lastEffect.nextEffect = fiber
            }
            if(!parent.firstEffect){
                parent.firstEffect = fiber.firstEffect || fiber
            }
            parent.lastEffect = fiber
        }
    }
}
let i = 0
function commitRoot(fiber) {
    console.log(fiber)
    let currentFiber = fiber.firstEffect
    while (currentFiber) {
        // console.log(currentFiber)
        currentFiber.parent.stateNode.appendChild(currentFiber.stateNode)
        currentFiber = currentFiber.nextEffect
        if(i>100){
            console.log('max')
            return
        }
        i++
    }
}

function beginWork(fiber) {
    // 创建DOM
    if (!fiber.stateNode) {
        fiber.stateNode = createDOM(fiber)
    }
    // 为DOM添加属性
    for (let attr in fiber.props) {
        if (attr !== 'children') {
            fiber.stateNode[attr] = fiber.props[attr]
        }
    }

    // 创建fiber的子级fiber
    let children = fiber.props.children
    if (Array.isArray(children)) {
        let prevFiber
        children.forEach((child, index) => {
            let childFiber = {
                type: child.type,
                props: child.props,
                effectTag: 'placement',
                effects: [],
                parent: fiber
            }
            if (index === 0) {
                fiber.child = childFiber
            } else {
                prevFiber.subling = childFiber
            }
            prevFiber = childFiber
        })
    }
}

export function render(virtualDOM, container) {
    nextUnitOfWork = {
        stateNode: container,
        props: {
            children: [virtualDOM]
        },
        effects: []
    }
    requestIdleCallback(workLoop)
}

export default {
    createElement
}